package cs383.GradUnit;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;



public class aGame extends Activity {
	
	private static final int dealerButtonWidth = 30;
	private static final int dealerButtonHeight = 30;
	private static final int STARTINGCHIPS = 1000;
	private static final int MIN_BET = 15;
	private static final int HUMAN_PLAYER = 0;
	private static final String CURRENTPOT = "Current Pot: $"; 
	private static final String CHIPS = "$";
	private static final int TIME_DELAY = 4000;
	private static final int TIME_AFTER_BETTING_ROUND = 4000;
	
	private enum GameStates {NEWHAND, PREFLOP, ROUND1BETTING,ROUND2BETTING,ROUND3BETTING,ROUND4BETTING, 
		FLOP, TURN, RIVER, AWARDPOT, PLAYERWON, PLAYERLOST, USERINPUT};
	GameStates GameState, LastGameState;
	
	AbsoluteLayout gameLayout, lytPlayer0,lytPlayer1,lytPlayer2,lytPlayer3;
	Button btnCall, btnCheck, btnFold, btnBet, btnPause;
	SeekBar sldrBettingSlider;
	OnClickListener listenerCall, listenerCheck, listenerFold, listenerBet, listenerPause;
	OnSeekBarChangeListener listenerBettingSlider;
	ImageView imgDealerButton, imgPlayer0Card1, imgPlayer0Card2,imgPlayer1Card1, imgPlayer1Card2;
	ImageView imgPlayer2Card1, imgPlayer2Card2,imgPlayer3Card1, imgPlayer3Card2;
	ImageView imgCommCard1, imgCommCard2, imgCommCard3, imgCommCard4, imgCommCard5;
	TextView txtCurrentPot, txtPlayer0Chips, txtPlayer1Chips, txtPlayer2Chips, txtPlayer3Chips, txtSliderValue;
	ArrayList<cPoint> dealerButtonPoints;
	ArrayList<cPlayer> playerList;
	
	int sliderBet;
	private cTable pokerTable;
	private Object x;
	
	
    /** Called when the activity is first created. */
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void onResume(){
    	super.onResume();
    	initialize();
    }
    public void onPause(){
    	super.onPause();
    }
    public void onDestroy(){
    	super.onDestroy();
    }
    public void onStop(){
    	super.onStop();
    }
    
    private void initialize(){
    	
    	lytPlayer0 = (AbsoluteLayout)findViewById(R.id.lytP0);
    	lytPlayer1 = (AbsoluteLayout)findViewById(R.id.lytP1);
    	lytPlayer2 = (AbsoluteLayout)findViewById(R.id.lytP2);
    	lytPlayer3 = (AbsoluteLayout)findViewById(R.id.lytP3);
    	
    	initializeGame();
    	
    	initializeText();
    	initializeButtons();
    	initializeCardImages();
    	
    	
    	initializeSlider();
    	initializeDealerButton();
    	moveDealerButton();
    	updateText();
    	
    	newHand();
    }
    /*  
     * Initializes the slider widget that is used to control player bet amounts 
     */
    private void initializeSlider(){
    	sliderBet = pokerTable.getMinBet();
    	sldrBettingSlider = (SeekBar)findViewById(R.id.aGame_sldrBettingSlider);
    	sldrBettingSlider.setMax(100);
    	listenerBettingSlider = new SeekBar.OnSeekBarChangeListener() {
			
			public void onStopTrackingTouch(SeekBar seekBar) {}
			public void onStartTrackingTouch(SeekBar seekBar) {}
			
			public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
				double percent = (double)sldrBettingSlider.getProgress();
				//sliderBet = (int) (playerList.get(0).chips*(percent/100));
				sliderBet = (int)(pokerTable.getPlayerChipAmount(HUMAN_PLAYER)*(percent/100));
				
				if (sliderBet < pokerTable.getMinBet()){
					sliderBet = pokerTable.getMinBet();
				}
				
				updateText();
			}
		};
		sldrBettingSlider.setOnSeekBarChangeListener(listenerBettingSlider);
		
    }
    /* 
     * Initializes the various Button widgets and creates the neccessary OnClickListeners for each button 
     */
    private void initializeButtons(){
    	
    	// Initialize call button
    	btnCall = (Button)findViewById(R.id.aGame_btnCall);
    	listenerCall = new Button.OnClickListener(){
    		public void onClick(View v) {
    			passHand();
    		}
        	
        };
        
        btnCall.setOnClickListener(listenerCall);
        
        // Initiailize check button
        btnCheck = (Button)findViewById(R.id.aGame_btnCheck);
        listenerCheck = new OnClickListener(){
        	public void onClick(View v) {
    			passHand();
    		}
        	
        };
        btnCheck.setOnClickListener(listenerCheck);
        
        //Initialize fold button
        btnFold = (Button)findViewById(R.id.aGame_btnFold);
        listenerFold = new OnClickListener(){
        	public void onClick(View v) {
        		pokerTable.fold(0);
        		updateCardImagesOnFold(0);
    			passHand();
    		}
        	
        };
        btnFold.setOnClickListener(listenerFold);
        
        //Initialize bet button
        btnBet = (Button)findViewById(R.id.aGame_btnBet);
        listenerBet = new OnClickListener(){
        	public void onClick(View v) {
    			//playerList.get(0).chips -= sliderBet;
    			//currentPot += sliderBet;
    			
    			pokerTable.placeHumanBet(sliderBet);
    			updateText();
    		}
        	
        };
        btnBet.setOnClickListener(listenerBet);
        
        //Initialize pause button
        btnPause = (Button)findViewById(R.id.aGame_btnPause);
        listenerPause = new OnClickListener(){
        	public void onClick(View v) {
    			
    		}
        	
        };
        btnPause.setOnClickListener(listenerPause);
    }
    /* Creates an instance of cTable() to act as the game manager and initializes and game specific values such 
     * as the minimum bet amount
     */
    private void initializeGame(){
    	
    	pokerTable = new cTable();
    	pokerTable.setMinBet(MIN_BET);
    	
    }
    /* Initializes the TextViews and sets their starting values
     * The TextViews are the current pot, the chip amounts for each player
     *   and labels for the betting slider
     */
    private void initializeText(){
    	txtCurrentPot = (TextView)findViewById(R.id.aGame_textCurrentPot);
    	txtCurrentPot.setText(CURRENTPOT);
    	
    	txtPlayer0Chips = (TextView)findViewById(R.id.aGame_txtPlayer0Chips);
    	txtPlayer0Chips.setText(CHIPS);
    	txtPlayer1Chips = (TextView)findViewById(R.id.aGame_txtPlayer1Chips);
    	txtPlayer1Chips.setText(CHIPS);
    	txtPlayer2Chips = (TextView)findViewById(R.id.aGame_txtPlayer2Chips);
    	txtPlayer2Chips.setText(CHIPS);
    	txtPlayer3Chips = (TextView)findViewById(R.id.aGame_txtPlayer3Chips);
    	txtPlayer3Chips.setText(CHIPS);
    	
    	txtSliderValue = (TextView)findViewById(R.id.aGame_txtSliderValue);
    	txtSliderValue.setText(CHIPS+pokerTable.getMinBet());
    }
    /* Initializes the ImageViews that represent each players' cards and the community cards
     * The cards are initialized to be invisible
     *    They are made visible when 'dealt' to simulate the dealing action
     */
    private void initializeCardImages(){
    	imgPlayer0Card1 = (ImageView)findViewById(R.id.imgP0C1); imgPlayer0Card1.setVisibility(View.INVISIBLE);
    	imgPlayer0Card2 = (ImageView)findViewById(R.id.imgP0C2); imgPlayer0Card2.setVisibility(View.INVISIBLE);
    	
    	imgPlayer1Card1 = (ImageView)findViewById(R.id.imgP1C1); imgPlayer1Card1.setVisibility(View.INVISIBLE);
    	imgPlayer1Card2 = (ImageView)findViewById(R.id.imgP1C2); imgPlayer1Card2.setVisibility(View.INVISIBLE);
    	
    	imgPlayer2Card1 = (ImageView)findViewById(R.id.imgP2C1); imgPlayer2Card1.setVisibility(View.INVISIBLE);
    	imgPlayer2Card2 = (ImageView)findViewById(R.id.imgP2C2); imgPlayer2Card2.setVisibility(View.INVISIBLE);
    	
    	imgPlayer3Card1 = (ImageView)findViewById(R.id.imgP3C1); imgPlayer3Card1.setVisibility(View.INVISIBLE);
    	imgPlayer3Card2 = (ImageView)findViewById(R.id.imgP3C2); imgPlayer3Card2.setVisibility(View.INVISIBLE);
    	
    	imgCommCard1 = (ImageView)findViewById(R.id.imgComm1); imgCommCard1.setVisibility(View.INVISIBLE);
    	imgCommCard2 = (ImageView)findViewById(R.id.imgComm2); imgCommCard2.setVisibility(View.INVISIBLE);
    	imgCommCard3 = (ImageView)findViewById(R.id.imgComm3); imgCommCard3.setVisibility(View.INVISIBLE);
    	imgCommCard4 = (ImageView)findViewById(R.id.imgComm4); imgCommCard4.setVisibility(View.INVISIBLE);
    	imgCommCard5 = (ImageView)findViewById(R.id.imgComm5); imgCommCard5.setVisibility(View.INVISIBLE);
    	
    }
    /*
     * Initializes the ImageView for the dealer button
     * Creates a list of cPoint objects to store the coordinates of each dealer button position (1 for each player)
     * The initial position of the button image is set by getting the position of the dealer button from the 
     *   cTable object pokerTable.  That player index is used to get the correct cPoint object from the list
     *   and then positions the image using the stored coordinates
     */
    private void initializeDealerButton(){
    	gameLayout = (AbsoluteLayout) findViewById(R.id.aGame_Layout);
    	imgDealerButton = new ImageView(this);
    	imgDealerButton.setImageDrawable(getResources().getDrawable(android.R.drawable.presence_invisible));
    	
    	AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(dealerButtonWidth, dealerButtonHeight, 0, 0);
    	imgDealerButton.setLayoutParams(layoutParams);
    	
    	gameLayout.addView(imgDealerButton);
    	
    	dealerButtonPoints = new ArrayList<cPoint>();
    	
    	cPoint point2 = new cPoint(497,180);
    	cPoint point0 = new cPoint(497,300);
    	cPoint point1 = new cPoint(175,250);
    	cPoint point3 = new cPoint(687,250);
    	
    	dealerButtonPoints.add(point0);
    	dealerButtonPoints.add(point1);
    	dealerButtonPoints.add(point2);
    	dealerButtonPoints.add(point3);
    	
    	imgDealerButton.setX(dealerButtonPoints.get(pokerTable.getDealerButton()).getX());
    	imgDealerButton.setY(dealerButtonPoints.get(pokerTable.getDealerButton()).getY());
    	
    	
    	Log.d("initializeGame", "Starting dealer button position is "+pokerTable.getDealerButton());
    }
    
   /*
    * Uses pokerTable (cTable object) to move the position of the dealer button 1 player clockwise
    * The new player index is used to update the layout coordinates of the image
    * 
    */
    private void moveDealerButton(){
    	
    	pokerTable.moveDealerButton();
    	
    	imgDealerButton.setX(dealerButtonPoints.get(pokerTable.getDealerButton()).getX());
    	imgDealerButton.setY(dealerButtonPoints.get(pokerTable.getDealerButton()).getY());
    	
    	updateText();
   }
    /*
     * passHand() is called after the human player makes a decision
     * It gets the current betting round number from pokerTable (cTable object) and calls the appropriate betting function
     * 		This serves to restart the game flow after it is suspended to turn control back to the human player
     */
    private void passHand(){
    	if(pokerTable.getBettingRound() == 1)
    		round1BettingAfterPlayer();
    	if(pokerTable.getBettingRound() == 2)
    		round2BettingAfterPlayer();
    	if(pokerTable.getBettingRound() == 3)
    		round3BettingAfterPlayer();
    	if(pokerTable.getBettingRound() == 4)
    		round4BettingAfterPlayer();
    }
    
    
    		
    		
    private void newHand(){
    	disablePlayerControls();
    	pokerTable.handInitialization();
    	clearCurrentPlayerIndicator();
    	preFlop();
    }
    
    private void preFlop(){
    	moveDealerButton();
    	setCurrentPlayerIndicator(pokerTable.getDealerButton() + 1);
    	dealHand();
    }
    
    private void dealHand(){
    	Handler mHandler = new Handler();
    	Runnable dealPlayer0Card1 = new Runnable() {
    	    public void run() {
    	    	imgPlayer0Card1.setVisibility(View.VISIBLE);
    	    	imgPlayer0Card1.invalidate();
    	    }
    	};
    	Runnable dealPlayer0Card2 = new Runnable() {
    	    public void run() {
    	    	imgPlayer0Card2.setVisibility(View.VISIBLE);
    	    	imgPlayer0Card2.invalidate();
    	    }
    	};
    	Runnable dealPlayer1Card1 = new Runnable() {
    	    public void run() {
    	    	imgPlayer1Card1.setVisibility(View.VISIBLE);
    	    	imgPlayer1Card1.invalidate();
    	    }
    	};
    	Runnable dealPlayer1Card2 = new Runnable() {
    	    public void run() {
    	    	imgPlayer1Card2.setVisibility(View.VISIBLE);
    	    	imgPlayer1Card2.invalidate();
    	    }
    	};
    	Runnable dealPlayer2Card1 = new Runnable() {
    	    public void run() {
    	    	imgPlayer2Card1.setVisibility(View.VISIBLE);
    	    	imgPlayer2Card1.invalidate();
    	    }
    	};
    	Runnable dealPlayer2Card2 = new Runnable() {
    	    public void run() {
    	    	imgPlayer2Card2.setVisibility(View.VISIBLE);
    	    	imgPlayer2Card2.invalidate();
    	    }
    	};
    	Runnable dealPlayer3Card1 = new Runnable() {
    	    public void run() {
    	    	imgPlayer3Card1.setVisibility(View.VISIBLE);
    	    	imgPlayer3Card1.invalidate();
    	    }
    	};
    	Runnable dealPlayer3Card2 = new Runnable() {
    	    public void run() {
    	    	imgPlayer3Card2.setVisibility(View.VISIBLE);
    	    	imgPlayer3Card2.invalidate();
    	    }
    	};
    	
    	
    	int order = 0;
    	for(int i = pokerTable.getDealerButton(); 
    			i < pokerTable.getDealerButton()+pokerTable.getNumberOfActivePlayers();
    			i++)
    	{	
    		
    		int timeOffset2 = order*500;
    		if(timeOffset2 == 0){
    			timeOffset2 = pokerTable.getNumberOfActivePlayers() * 500;
    		}
    		else{
    			timeOffset2 = pokerTable.getNumberOfActivePlayers() * 500 + order*500;
    		}
	    	int currentCardsToUpdate = i+1;
    			if(currentCardsToUpdate % pokerTable.getNumberOfActivePlayers() == 0){
    				mHandler.postDelayed(dealPlayer0Card1, (order*500));
    				mHandler.postDelayed(dealPlayer0Card2,  timeOffset2);
    			}
    			if(currentCardsToUpdate % pokerTable.getNumberOfActivePlayers() == 1){
    				mHandler.postDelayed(dealPlayer1Card1, (order*500));
    				mHandler.postDelayed(dealPlayer1Card2, timeOffset2);
    			}
    			if(currentCardsToUpdate % pokerTable.getNumberOfActivePlayers() == 2){
    				mHandler.postDelayed(dealPlayer2Card1, (order*500));
    				mHandler.postDelayed(dealPlayer2Card2,  timeOffset2);
    			}
    			if(currentCardsToUpdate % pokerTable.getNumberOfActivePlayers() == 3){
    				mHandler.postDelayed(dealPlayer3Card1, (order*500));
    				mHandler.postDelayed(dealPlayer3Card2,  timeOffset2);
    			}
    				
    			order++;
    	}
    	
    	Runnable moveToBetting = new Runnable() {
    	    public void run() {
    	    	//round1BettingBeforePlayer();
    	    	bettingBeforePlayer();
    	    }
    	};
    	mHandler.postDelayed(moveToBetting, TIME_AFTER_BETTING_ROUND);
    	
    }
    
    private void flop(){
    	pokerTable.incrementBettingRound();
    	Log.d("flop", "The Flop just happend");
    	Toast.makeText(this, "Round " + pokerTable.getCurrentRoundNumber() + " Flop", Toast.LENGTH_SHORT).show();
    	
    	Handler mHandler = new Handler();
    	Runnable dealFlop1 = new Runnable() {
    	    public void run() {
    	    	imgCommCard1.setVisibility(View.VISIBLE);
    	    	imgCommCard1.invalidate();
    	    }
    	};
    	Runnable dealFlop2 = new Runnable() {
    	    public void run() {
    	    	imgCommCard2.setVisibility(View.VISIBLE);
    	    	imgCommCard2.invalidate();
    	    }
    	};
    	Runnable dealFlop3 = new Runnable() {
    	    public void run() {
    	    	imgCommCard3.setVisibility(View.VISIBLE);
    	    	imgCommCard3.invalidate();
    	    }
    	};
    	Runnable moveToBetting = new Runnable() {
    	    public void run() {
    	    	round2BettingBeforePlayer();
    	    }
    	};
    	
    	mHandler.postDelayed(dealFlop1, 750);
    	mHandler.postDelayed(dealFlop2, 1500);
    	mHandler.postDelayed(dealFlop3, 2250);
    	mHandler.postDelayed(moveToBetting, 3000);
    	
    }
    private void turn(){
    	pokerTable.incrementBettingRound();
    	Log.d("turn", "The Turn just happend");
    	Toast.makeText(this, "Round " + pokerTable.getCurrentRoundNumber() + " Turn", Toast.LENGTH_SHORT).show();
    	
    	Handler mHandler = new Handler();
    	Runnable dealTurn1 = new Runnable() {
    	    public void run() {
    	    	imgCommCard4.setVisibility(View.VISIBLE);
    	    	imgCommCard4.invalidate();
    	    }
    	};
    	Runnable moveToBetting = new Runnable() {
    	    public void run() {
    	    	round3BettingBeforePlayer();
    	    }
    	};
    	
    	mHandler.postDelayed(dealTurn1, 750);
    	mHandler.postDelayed(moveToBetting, 1500);
    }
    private void river(){
    	pokerTable.incrementBettingRound();
    	Log.d("turn", "The River just happend");
    	Toast.makeText(this, "Round " + pokerTable.getCurrentRoundNumber() + " River", Toast.LENGTH_SHORT).show();
    	
    	Handler mHandler = new Handler();
    	Runnable dealRiver = new Runnable() {
    	    public void run() {
    	    	imgCommCard5.setVisibility(View.VISIBLE);
    	    	imgCommCard5.invalidate();
    	    }
    	};
    	Runnable moveToBetting = new Runnable() {
    	    public void run() {
    	    	round4BettingBeforePlayer();
    	    }
    	};
    	
    	mHandler.postDelayed(dealRiver, 750);
    	mHandler.postDelayed(moveToBetting, 1500);
    }
    
    void makePlayerDecision(int playerNum){
		
    	double playerDecision = pokerTable.getPlayerDecision(playerNum);
    	Toast.makeText(aGame.this, "They chose " + playerDecision, Toast.LENGTH_SHORT).show();
	}
    
    public class myRunnable implements Runnable{
    	private int playerNum;
    	  public myRunnable(int curPlayerNum) {
    	    this.playerNum = curPlayerNum;
    	  }
    	  
		public void run() {
			Log.d("decision",  "Player " + playerNum + " making decision");
	    	final double lastPlayersChoice = pokerTable.getPlayerDecision(playerNum);
	    	Toast.makeText(aGame.this, "They chose " + lastPlayersChoice, Toast.LENGTH_SHORT).show();
	    	updateCurrentPlayerIndicator(playerNum);
		}
    }
    
    private void bettingBeforePlayer(){  //preflop
    	Handler mHandler = new Handler();
    	boolean usersTurn = false;
    	int playersThatHaveActed = 0;
    	
    	for(int i = pokerTable.getDealerButton()+1; i <= pokerTable.getDealerButton() + pokerTable.getNumberOfPlayersInHand(); i++ ){
    		int playerToAct = i % pokerTable.getNumberOfPlayersInHand();
			if(playerToAct == 0){
				usersTurn = true;
				break;
			}
			
			final int playerNum = playerToAct;
			playersThatHaveActed++;
			
			myRunnable playerDecision = new myRunnable(playerNum);
			mHandler.postDelayed(playerDecision, playersThatHaveActed * TIME_DELAY);
	    }	
    	
    	if(usersTurn == true){
    		Runnable switchToUsersTurn = new Runnable() {
        	    public void run() {
        	    	if(pokerTable.checkPlayerInHand(0)){
        				enablePlayerControls();
        				updateCurrentPlayerIndicator(0);
        	    	}
        			else{
        				if(pokerTable.getBettingRound() == 1)
        					round1BettingAfterPlayer();
        			}
        	    }
    		};
    		mHandler.postDelayed(switchToUsersTurn, (playersThatHaveActed+1)*TIME_DELAY);
    	}
    }
    private void round1BettingBeforePlayer(){  //preflop
    	Handler mHandler = new Handler();
    	boolean usersTurn = false;
    	int playersThatHaveActed = 0;
    	
    	for(int i = pokerTable.getDealerButton()+1; i <= pokerTable.getDealerButton() + pokerTable.getNumberOfPlayersInHand(); i++ ){
    		int playerToAct = i % pokerTable.getNumberOfPlayersInHand();
			if(playerToAct == 0){
				usersTurn = true;
				break;
			}
			
			final int playerNum = playerToAct;
			playersThatHaveActed++;
			
			myRunnable playerDecision = new myRunnable(playerNum);
			mHandler.postDelayed(playerDecision, playersThatHaveActed * TIME_DELAY);
	    }	
    	
    	if(usersTurn == true){
    		Runnable switchToUsersTurn = new Runnable() {
        	    public void run() {
        	    	if(pokerTable.checkPlayerInHand(0)){
        				enablePlayerControls();
        				updateCurrentPlayerIndicator(0);
        	    	}
        			else{
        				round1BettingAfterPlayer();
        			}
        	    }
    		};
    		mHandler.postDelayed(switchToUsersTurn, (playersThatHaveActed+1)*TIME_DELAY);
    	}
    }
    
    private void round1BettingAfterPlayer(){  //preflop
    	disablePlayerControls();
    	Handler mHandler = new Handler();
    	
    	int playersThatHaveActed = 0;
    	for(int i = 1; i <= pokerTable.getDealerButton(); i++ ){
    		
			
			final int playerNum = i;
			myRunnable playerDecision = new myRunnable(playerNum);
			mHandler.postDelayed(playerDecision, playersThatHaveActed * TIME_DELAY);
			
	    	playersThatHaveActed++;  /* increment after in this betting loop so as soon as the user makes a decision
			it appears that the next player is thinking */
		}
    	
    	Runnable dealFlop = new Runnable() {
    	    public void run() {
    	    	if(pokerTable.getNumberOfActivePlayers() > 1){
    				flop();
    	    	}
    			else
    				awardPot();
    	    }
		};
    	mHandler.postDelayed(dealFlop, (playersThatHaveActed+1)*TIME_AFTER_BETTING_ROUND);
    }
    
    private void round2BettingBeforePlayer(){  //flop
    	Handler mHandler = new Handler();
    	boolean usersTurn = false;
    	int playersThatHaveActed = 0;
    	
    	for(int i = pokerTable.getDealerButton()+1; i <= pokerTable.getDealerButton() + pokerTable.getNumberOfPlayersInHand(); i++ ){
    		int playerToAct = i % pokerTable.getNumberOfPlayersInHand();
			if(playerToAct == 0){
				usersTurn = true;
				break;
			}
			
			final int playerNum = playerToAct;
			playersThatHaveActed++;
			
			myRunnable playerDecision = new myRunnable(playerNum);
			mHandler.postDelayed(playerDecision, playersThatHaveActed * TIME_DELAY);
	    }	
    	
    	if(usersTurn == true){
    		Runnable switchToUsersTurn = new Runnable() {
        	    public void run() {
        	    	if(pokerTable.checkPlayerInHand(0)){
        				enablePlayerControls();
        				updateCurrentPlayerIndicator(0);
        	    	}
        			else{
        				round2BettingAfterPlayer();
        			}
        	    }
    		};
    		mHandler.postDelayed(switchToUsersTurn, (playersThatHaveActed+1)*4000);
    	}
    }
	    	
    
    private void round2BettingAfterPlayer(){  //flop
    	disablePlayerControls();
    	Handler mHandler = new Handler();
    	
    	int playersThatHaveActed = 0;
    	
    	Runnable switchToUsersTurn = new Runnable() {
    	    public void run() {
    	    	if(pokerTable.getNumberOfActivePlayers() > 1){
    				turn();
    	    	}
    			else
    				awardPot();
    	    }
		};
    	
    	for(int i = 1; i <= pokerTable.getDealerButton(); i++ ){
    		final int playerNum = i;
			myRunnable playerDecision = new myRunnable(playerNum);
			mHandler.postDelayed(playerDecision, playersThatHaveActed * TIME_DELAY);
	    	playersThatHaveActed++;  /* increment after in this betting loop so as soon as the user makes a decision
			it appears that the next player is thinking */
		}
    	
    	mHandler.postDelayed(switchToUsersTurn, (playersThatHaveActed+1)*4000);
    }
    	
    
    private void round3BettingBeforePlayer(){  //turn
    	Handler mHandler = new Handler();
    	boolean usersTurn = false;
    	int playersThatHaveActed = 0;
    	
    	for(int i = pokerTable.getDealerButton()+1; i <= pokerTable.getDealerButton() + pokerTable.getNumberOfPlayersInHand(); i++ ){
    		int playerToAct = i % pokerTable.getNumberOfPlayersInHand();
			if(playerToAct == 0){
				usersTurn = true;
				break;
			}
			
			final int playerNum = playerToAct;
			playersThatHaveActed++;
			myRunnable playerDecision = new myRunnable(playerNum);
			mHandler.postDelayed(playerDecision, playersThatHaveActed * TIME_DELAY);
	    }	
    	
    	if(usersTurn == true){
    		Runnable switchToUsersTurn = new Runnable() {
        	    public void run() {
        	    	if(pokerTable.checkPlayerInHand(0)){
        				enablePlayerControls();
        				updateCurrentPlayerIndicator(0);
        	    	}
        			else{
        				round3BettingAfterPlayer();
        			}
        	    }
    		};
    		mHandler.postDelayed(switchToUsersTurn, (playersThatHaveActed+1)*4000);
    	}
    }
    private void round3BettingAfterPlayer(){  //turn
    	disablePlayerControls();
    	Handler mHandler = new Handler();
    	
    	int playersThatHaveActed = 0;
    	
    	Runnable switchToUsersTurn = new Runnable() {
    	    public void run() {
    	    	if(pokerTable.getNumberOfActivePlayers() > 1){
    				river();
    	    	}
    			else
    				awardPot();
    	    }
		};
    	
    	for(int i = 1; i <= pokerTable.getDealerButton(); i++ ){
    		final int playerNum = i;
			myRunnable playerDecision = new myRunnable(playerNum);
			mHandler.postDelayed(playerDecision, playersThatHaveActed * TIME_DELAY);
	    	playersThatHaveActed++;  /* increment after in this betting loop so as soon as the user makes a decision
			it appears that the next player is thinking */
		}
    	
    	mHandler.postDelayed(switchToUsersTurn, (playersThatHaveActed+1)*4000);
    }
    private void round4BettingBeforePlayer(){  //river
    	Handler mHandler = new Handler();
    	boolean usersTurn = false;
    	int playersThatHaveActed = 0;
    	
    	for(int i = pokerTable.getDealerButton()+1; i <= pokerTable.getDealerButton() + pokerTable.getNumberOfPlayersInHand(); i++ ){
    		int playerToAct = i % pokerTable.getNumberOfPlayersInHand();
			if(playerToAct == 0){
				usersTurn = true;
				break;
			}
			
			final int playerNum = playerToAct;
			playersThatHaveActed++;
			myRunnable playerDecision = new myRunnable(playerNum);
			mHandler.postDelayed(playerDecision, playersThatHaveActed * TIME_DELAY);
	    }	
    	
    	if(usersTurn == true){
    		Runnable switchToUsersTurn = new Runnable() {
        	    public void run() {
        	    	if(pokerTable.checkPlayerInHand(0)){
        				enablePlayerControls();
        				updateCurrentPlayerIndicator(0);
        	    	}
        			else{
        				round4BettingAfterPlayer();
        			}
        	    }
    		};
    		mHandler.postDelayed(switchToUsersTurn, (playersThatHaveActed+1)*4000);
    	}
    }
    private void round4BettingAfterPlayer(){  //river
    	disablePlayerControls();
    	Handler mHandler = new Handler();
    	
    	int playersThatHaveActed = 0;
    	
    	for(int i = 1; i <= pokerTable.getDealerButton(); i++ ){
    		
			
			final int playerNum = i;
			myRunnable playerDecision = new myRunnable(playerNum);
			mHandler.postDelayed(playerDecision, playersThatHaveActed * TIME_DELAY);
	    	playersThatHaveActed++;  /* increment after in this betting loop so as soon as the user makes a decision
			it appears that the next player is thinking */
		}
    	
    	Runnable switchToUsersTurn = new Runnable() {
    	    public void run() {
    	    	awardPot();
    	    }
		};
    	mHandler.postDelayed(switchToUsersTurn, (playersThatHaveActed+1)*4000);
    }
    	
    private void awardPot(){
    	pokerTable.awardPot();
    	
    	Toast.makeText(this, "Round " + pokerTable.getCurrentRoundNumber() + " Awarding Pot", Toast.LENGTH_LONG).show();
    	
    	Handler mHandler = new Handler();
    	Runnable updateGUI = new Runnable(){
    		public void run(){
    			updateText();
    	    	clearPlayerCardImages();
    	    	clearCommunityCardImages();
    		}
    	};
    	mHandler.postDelayed(updateGUI, 2000);
    	Runnable endOfHand = new Runnable(){
    		public void run(){
    			if(pokerTable.getNumberOfActivePlayers() == 1){
    				// add end game code here
    			}
    			else{
    				newHand();
    			}
    		}
    	};
    	mHandler.postDelayed(endOfHand, 2500);
    	
    	
    }
    
    
    
    
    
    private void updateText(){
    	txtCurrentPot.setText(CURRENTPOT+pokerTable.getCurrentPot());
    	
    	
    	txtPlayer0Chips.setText(CHIPS+pokerTable.getPlayerChipAmount(HUMAN_PLAYER));
    	
    	txtPlayer1Chips.setText(CHIPS+pokerTable.getPlayerChipAmount(1));
    	
    	txtPlayer2Chips.setText(CHIPS+pokerTable.getPlayerChipAmount(2));
    	
    	txtPlayer3Chips.setText(CHIPS+pokerTable.getPlayerChipAmount(3));
    	
    	txtSliderValue.setText(CHIPS+sliderBet);
    }
    
    private void clearPlayerCardImages(){
    	
    	imgPlayer0Card1.setVisibility(View.INVISIBLE);
    	imgPlayer0Card2.setVisibility(View.INVISIBLE);
    	
    	imgPlayer1Card1.setVisibility(View.INVISIBLE);
    	imgPlayer1Card2.setVisibility(View.INVISIBLE);
    	
    	imgPlayer2Card1.setVisibility(View.INVISIBLE);
    	imgPlayer2Card2.setVisibility(View.INVISIBLE);
    	
    	imgPlayer3Card1.setVisibility(View.INVISIBLE);
    	imgPlayer3Card2.setVisibility(View.INVISIBLE);
    }
    private void updateCardImagesOnFold(int playerNum){
    	if(playerNum == 0){
    		imgPlayer0Card1.setVisibility(View.INVISIBLE);
    		imgPlayer0Card2.setVisibility(View.INVISIBLE);
    	}
    	else if(playerNum == 1){
    		imgPlayer1Card1.setVisibility(View.INVISIBLE);
    		imgPlayer1Card2.setVisibility(View.INVISIBLE);
    	}
    	else if (playerNum == 2){
    		imgPlayer2Card1.setVisibility(View.INVISIBLE);
    		imgPlayer2Card2.setVisibility(View.INVISIBLE);
    	}
    	else if(playerNum == 3){
    		imgPlayer3Card1.setVisibility(View.INVISIBLE);
    		imgPlayer3Card2.setVisibility(View.INVISIBLE);
    	}
    }
    private void clearCommunityCardImages(){
    	imgCommCard1.setVisibility(View.INVISIBLE);
    	imgCommCard2.setVisibility(View.INVISIBLE);
    	imgCommCard3.setVisibility(View.INVISIBLE);
    	imgCommCard4.setVisibility(View.INVISIBLE);
    	imgCommCard5.setVisibility(View.INVISIBLE);
    }
    private void enablePlayerControls(){
    	btnCall.setEnabled(true);
    	btnCheck.setEnabled(true);
    	btnFold.setEnabled(true);
    	btnBet.setEnabled(true);
    	btnPause.setEnabled(true);
    	sldrBettingSlider.setEnabled(true);
    }
    private void disablePlayerControls(){
    	btnCall.setEnabled(false);
    	btnCheck.setEnabled(false);
    	btnFold.setEnabled(false);
    	btnBet.setEnabled(false);
    	btnPause.setEnabled(false);
    	sldrBettingSlider.setEnabled(false);
    }
    
    private void updateCurrentPlayerIndicator(int playerNum){
    	//Log.d("update",  "updateCurrentPlayerIndicator");
    	clearCurrentPlayerIndicator();
    	if (playerNum == 0){
    		//lytPlayer3.setBackgroundColor(getResources().getColor(android.R.color.black));
    		lytPlayer0.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
    	}
    	else if(playerNum == 1){
    		//lytPlayer0.setBackgroundColor(getResources().getColor(android.R.color.black));
    		lytPlayer1.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
    	}
    	else if (playerNum == 2){
    		//lytPlayer1.setBackgroundColor(getResources().getColor(android.R.color.black));
    		lytPlayer2.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
    	}
    	else if (playerNum == 3){
    		//lytPlayer2.setBackgroundColor(getResources().getColor(android.R.color.black));
    		lytPlayer3.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
    	}
    }
    
    private void setCurrentPlayerIndicator(int playerNum){
    	//Log.d("update",  "updateCurrentPlayerIndicator");
    	if (playerNum == 0){
    		lytPlayer0.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
    	}
    	else if(playerNum == 1){
    		lytPlayer1.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
    	}
    	else if (playerNum == 2){
    		lytPlayer2.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
    	}
    	else if (playerNum == 3){
    		lytPlayer3.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
    	}
    }
    
    private void clearCurrentPlayerIndicator(){
    	lytPlayer0.setBackgroundColor(getResources().getColor(android.R.color.black));
    	lytPlayer1.setBackgroundColor(getResources().getColor(android.R.color.black));
    	lytPlayer2.setBackgroundColor(getResources().getColor(android.R.color.black));
    	lytPlayer3.setBackgroundColor(getResources().getColor(android.R.color.black));
    }
    
     
}
